       
    create Table SystemInfo
    (
    	ServerCN nvarchar(200)
    		PRIMARY KEY NOT NULL,	
    	Loaded datetime
    		DEFAULT(GETDATE()) NOT NULL
    )
    go

    create table Report
    (	
	ID bigint
    		PRIMARY KEY,
 	OrganizationId uniqueidentifier
    		NOT NULL,
	EntityManagerOwningUser uniqueidentifier
    		NOT NULL,
	ReportId uniqueidentifier
		NOT NULL,
	FileName varchar(255)
		NOT NULL,
	ReportTypeCode int 
		NOT NULL,
	State int 
    		DEFAULT(0) NOT NULL,
	TimeStamp datetime
    		DEFAULT(GETDATE()) NOT NULL
    )
    go
    Create Trigger TimeStampUpdateReport
    	on Report
    AFTER UPDATE AS
    	UPDATE Report set TimeStamp=GetDate() WHERE ID in (select id from inserted)
    go

    create table TransactionCurrency
    (	
	ID bigint
    		PRIMARY KEY,
 	OrganizationId uniqueidentifier
    		NOT NULL,
	EntityManagerOwningUser uniqueidentifier
    		NOT NULL,
	TransactionCurrencyId uniqueidentifier
		NOT NULL,
	CurrencyName varchar(255) 
		NOT NULL,
	CurrencySymbol varchar(30),
	ISOCurrencyCode varchar(30)
		NOT NULL,
        State int 
    		DEFAULT(0) NOT NULL,
	TimeStamp datetime
    		DEFAULT(GETDATE()) NOT NULL
    )
    go
    Create Trigger TimeStampUpdateTransactionCurrency
    	on TransactionCurrency
    AFTER UPDATE AS
    	UPDATE TransactionCurrency set TimeStamp=GetDate() WHERE ID in (select id from inserted)
    go    

    create table SystemUser
    (
    	ID bigint PRIMARY KEY,
 	OrganizationId uniqueidentifier
    		NOT NULL,
	OrganizationName varchar(128),
	EntityManagerOwningUser uniqueidentifier
    		NOT NULL,
    	DomainName varchar(255)
    		 NOT NULL,
    	UserPassword varchar(32),
    	SystemUserId uniqueidentifier 
    		 NOT NULL,
    	BusinessUnitId uniqueidentifier
    		NOT NULL,
        Role varchar(255),
	CrmServer varchar(255),
	DiscoveryServer varchar(255),
	ApiServer varchar(255),
	Partner varchar(255),	
	Puid varchar(255),
	PassportName varchar(128),
	PassportTicket varchar(4096),
	PassportTicketExpiration varchar(128),
        CrmTicket varchar(4096),
        CrmTicketExpiration varchar(128),
    	State int 
    		DEFAULT(0) NOT NULL,
	OutlookState int 
    		DEFAULT(0) NOT NULL,
    	SetupUser bit
    		DEFAULT(0) NOT NULL,
	InternalEmailAddress varchar(255),
    	TimeStamp datetime
    		DEFAULT(GETDATE()) NOT NULL
    )
    go
    
    Create Trigger TimeStampUpdateSystemUser
    	on SystemUser
    AFTER UPDATE AS
    	UPDATE SystemUser set TimeStamp=GetDate() WHERE ID in (select id from inserted)
    go
    
    Create Table Organization
    (
    	ID bigint
    		PRIMARY KEY,
 	OrganizationId uniqueidentifier
    		 NOT NULL,
	EntityManagerOwningUser uniqueidentifier
    		NOT NULL,
	CrmServer varchar(255),
    	Tokenkey nvarchar(90),
    	State int 
    		DEFAULT(0) NOT NULL,
    	TimeStamp datetime
    		DEFAULT(GETDATE()) NOT NULL
    
    )
    GO

    Create Table FakeParentOrg
    (
    	ID bigint
    		PRIMARY KEY,
 	OrganizationId uniqueidentifier
    		 NOT NULL
    )
    GO
    
    Create Trigger TimeStampUpdateOrganization
    	on Organization
    AFTER UPDATE AS
    	UPDATE Organization set TimeStamp=GetDate() WHERE ID in (select id from inserted)
    GO
    
    Create Table Lead
    (
    	ID bigint
    		PRIMARY KEY,
    	OwningUser uniqueidentifier
    		NOT NULL,
	EntityManagerOwningUser uniqueidentifier
    		NOT NULL,
 	LeadId uniqueidentifier
    		 NOT NULL,
    	FirstName nvarchar(150),
    	LastName nvarchar(150),
    	State int 
    		DEFAULT(0) NOT NULL,
    	TimeStamp datetime
    		DEFAULT(GETDATE()) NOT NULL
    )
    GO
    
    Create Trigger TimeStampUpdateLead
    	on Lead
    AFTER UPDATE AS
    	UPDATE Lead set TimeStamp=GetDate() WHERE ID in (select id from inserted)
    GO
    
    Create Table Opportunity
    (
    	ID bigint
    		NOT NULL,
    	OwningUser uniqueidentifier
    		NOT NULL,
	EntityManagerOwningUser uniqueidentifier
    		NOT NULL,
 	OpportunityId uniqueidentifier
    		 NOT NULL,
    	Name nvarchar(160),
    	State int 
    		DEFAULT(0) NOT NULL,
    	TimeStamp datetime
    		DEFAULT(GETDATE()) NOT NULL
    )
    GO    
    
    Create Trigger TimeStampUpdateOpportunity
    	on Opportunity
    AFTER UPDATE AS
    	UPDATE Opportunity set TimeStamp=GetDate() WHERE ID in (select id from inserted)
    GO

    CREATE UNIQUE CLUSTERED INDEX [CL_ID_State] ON [dbo].[Opportunity] 
    (
	[ID] ASC,
	[State] ASC
    )WITH (SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF) ON [PRIMARY]
    
    Create Table Account
    (
    	ID bigint
    		PRIMARY KEY,
    	OwningUser uniqueidentifier
    		NOT NULL,
	EntityManagerOwningUser uniqueidentifier
    		NOT NULL,
 	AccountId uniqueidentifier
    		 NOT NULL,
    	Name nvarchar(160),
    	State int 
    		DEFAULT(0) NOT NULL,
    	TimeStamp datetime
    		DEFAULT(GETDATE()) NOT NULL
    )
    GO
    
    Create Trigger TimeStampUpdateAccount
    	on Account
    AFTER UPDATE AS
    	UPDATE Account set TimeStamp=GetDate() WHERE ID in (select id from inserted)
    GO

    Create Table New_Simple_CustomAccount
    (
    	ID bigint
    		PRIMARY KEY,
    	OwningUser uniqueidentifier
    		NOT NULL,
	EntityManagerOwningUser uniqueidentifier
    		NOT NULL,
 	New_Simple_CustomAccountId uniqueidentifier
    		 NOT NULL,
    	New_Name nvarchar(160),
    	State int 
    		DEFAULT(0) NOT NULL,
    	TimeStamp datetime
    		DEFAULT(GETDATE()) NOT NULL
    )
    GO
    
    Create Trigger TimeStampUpdateNew_Simple_CustomAccount
    	on New_Simple_CustomAccount
    AFTER UPDATE AS
    	UPDATE New_Simple_CustomAccount set TimeStamp=GetDate() WHERE ID in (select id from inserted)
    GO
    
    Create Table Contact
    (
    	ID bigint
    		PRIMARY KEY,
    	OwningUser uniqueidentifier
    		NOT NULL,
	EntityManagerOwningUser uniqueidentifier
    		NOT NULL,
 	ContactId uniqueidentifier
    		 NOT NULL,
    	LastName nvarchar(50),
    	State int 
    		DEFAULT(0) NOT NULL,
    	TimeStamp datetime
    		DEFAULT(GETDATE()) NOT NULL
    )
    GO
    
    Create Trigger TimeStampUpdateContact
    	on Contact
    AFTER UPDATE AS
    	UPDATE Contact set TimeStamp=GetDate() WHERE ID in (select id from inserted)
    GO
    
    Create Table GenericActivities
    (
    	ID bigint
    		PRIMARY KEY,
    	State int 
    		DEFAULT(0) NOT NULL,
    	TimeStamp datetime
    		DEFAULT(GETDATE()) NOT NULL
    )
    GO
    
    Create Trigger TimeStampUpdateGenericActivities
    	on GenericActivities
    AFTER UPDATE AS
    	UPDATE GenericActivities set TimeStamp=GetDate() WHERE ID in (select id from inserted)
    GO
    
    Create Table Service
    (
    	ID bigint
    		PRIMARY KEY,
 	OrganizationId uniqueidentifier
    		NOT NULL,
	EntityManagerOwningUser uniqueidentifier
    		NOT NULL,
 	ServiceId uniqueidentifier
    		 NOT NULL,
    	Name nvarchar(160),
    	State int 
    		DEFAULT(0) NOT NULL,
    	TimeStamp datetime
    		DEFAULT(GETDATE()) NOT NULL
    )
    GO
    
    Create Trigger TimeStampUpdateService
    	on Service
    AFTER UPDATE AS
    	UPDATE Service set TimeStamp=GetDate() WHERE ID in (select id from inserted)
    GO
    
    Create Table Subject
    (
    	ID bigint
    		PRIMARY KEY,
 	OrganizationId uniqueidentifier
    		NOT NULL,
	EntityManagerOwningUser uniqueidentifier
    		NOT NULL,
 	SubjectId uniqueidentifier
    		 NOT NULL,
    	Title nvarchar(500),
    	State int 
    		DEFAULT(0) NOT NULL,
    	TimeStamp datetime
    		DEFAULT(GETDATE()) NOT NULL
    )
    GO
    
    Create Trigger TimeStampUpdateSubject
    	on Subject
    AFTER UPDATE AS
    	UPDATE Subject set TimeStamp=GetDate() WHERE ID in (select id from inserted)
    GO
    
    Create Table Task
    (
    	ID bigint
    		PRIMARY KEY,
    	OwningUser uniqueidentifier
    		NOT NULL,
	EntityManagerOwningUser uniqueidentifier
    		NOT NULL,
 	ActivityId uniqueidentifier
    		 NOT NULL,
    	State int 
    		DEFAULT(0) NOT NULL,
    	TimeStamp datetime
    		DEFAULT(GETDATE()) NOT NULL
    	
    )
    GO
    
    Create Trigger TimeStampUpdateTask
    	on Task
    AFTER UPDATE AS
    	UPDATE Task set TimeStamp=GetDate() WHERE ID in (select id from inserted)
    GO
    
  Create Table Appointment
 (
  	ID bigint
  		PRIMARY KEY,
  	OwningUser uniqueidentifier
  		NOT NULL,
	EntityManagerOwningUser uniqueidentifier
    		NOT NULL,
  	ActivityId uniqueidentifier
  		 NOT NULL,
  	State int 
  		DEFAULT(0) NOT NULL,
  	TimeStamp datetime
  		DEFAULT(GETDATE()) NOT NULL
  	
  )
    GO
  
  Create Trigger TimeStampUpdateAppointment
  	on Appointment
  AFTER UPDATE AS
  	UPDATE Appointment set TimeStamp=GetDate() WHERE ID in (select id from inserted)
  GO
  
  Create Table Email
  (
  	ID bigint
  		PRIMARY KEY,
  	OwningUser uniqueidentifier
  		NOT NULL,
	EntityManagerOwningUser uniqueidentifier
    		NOT NULL,
  	ActivityId uniqueidentifier
  		 NOT NULL,
  	State int 
  		DEFAULT(0) NOT NULL,
  	TimeStamp datetime
  		DEFAULT(GETDATE()) NOT NULL
  	
  )
  GO
  
  Create Trigger TimeStampUpdateEmail
  	on Email
  AFTER UPDATE AS
  	UPDATE Email set TimeStamp=GetDate() WHERE ID in (select id from inserted)
  GO
  
  Create Table Fax
  (
  	ID bigint
  		PRIMARY KEY,
  	OwningUser uniqueidentifier
  		NOT NULL,
	EntityManagerOwningUser uniqueidentifier
    		NOT NULL,
  	ActivityId uniqueidentifier
  		 NOT NULL,
  	State int 
  		DEFAULT(0) NOT NULL,
  	TimeStamp datetime
  		DEFAULT(GETDATE()) NOT NULL
  	
  )
  GO
  
  Create Trigger TimeStampUpdateFax
  	on Fax
  AFTER UPDATE AS
  	UPDATE Fax set TimeStamp=GetDate() WHERE ID in (select id from inserted)
  GO
  
  Create Table Letter
  (
  	ID bigint
  		PRIMARY KEY,
  	OwningUser uniqueidentifier
  		NOT NULL,
	EntityManagerOwningUser uniqueidentifier
    		NOT NULL,
  	ActivityId uniqueidentifier
  		 NOT NULL,
  	State int 
  		DEFAULT(0) NOT NULL,
  	TimeStamp datetime
  		DEFAULT(GETDATE()) NOT NULL
  	
  )
  GO
  
  Create Trigger TimeStampUpdateLetter
  	on Letter
  AFTER UPDATE AS
  	UPDATE Letter set TimeStamp=GetDate() WHERE ID in (select id from inserted)
  GO
  
  Create Table Phonecall
  (
  	ID bigint
  		PRIMARY KEY,
  	OwningUser uniqueidentifier
  		NOT NULL,
	EntityManagerOwningUser uniqueidentifier
    		NOT NULL,
  	ActivityId uniqueidentifier
  		 NOT NULL,
  	State int 
  		DEFAULT(0) NOT NULL,
  	TimeStamp datetime
  		DEFAULT(GETDATE()) NOT NULL
  	
  )
  GO
  
  Create Trigger TimeStampUpdatePhonecall
  	on Phonecall
  AFTER UPDATE AS
  	UPDATE Phonecall set TimeStamp=GetDate() WHERE ID in (select id from inserted)
  GO
  
   Create Table Quote
    (
    	ID bigint
    		PRIMARY KEY,
 	OwnerId uniqueidentifier
    		NOT NULL,
	EntityManagerOwningUser uniqueidentifier
    		NOT NULL,
 	QuoteId uniqueidentifier
    		 NOT NULL,
    	Name nvarchar(300),
  	CustomerIdName nvarchar(160),
  	CustomerId uniqueidentifier,
  	CustomerIdType int,
    	State int 
    		DEFAULT(0) NOT NULL,
    	TimeStamp datetime
    		DEFAULT(GETDATE()) NOT NULL
    )
    GO
    
    Create Trigger TimeStampUpdateQuote
    	on Quote
    AFTER UPDATE AS
    	UPDATE Quote set TimeStamp=GetDate() WHERE ID in (select id from inserted)
    GO
    
    Create Table Template
    (
    	ID bigint
    		PRIMARY KEY,
 	OwnerId uniqueidentifier
    		NOT NULL,
	EntityManagerOwningUser uniqueidentifier
    		NOT NULL,
 	TemplateId uniqueidentifier
    		 NOT NULL,
    	TemplateTypeCode int
    		NOT NULL,
    	Subject ntext,
    	Title nvarchar(200),
    	State int 
    		DEFAULT(0) NOT NULL,
    	TimeStamp datetime
    		DEFAULT(GETDATE()) NOT NULL
    )
    GO
    
    Create Trigger TimeStampUpdateTemplate
    	on Template
    AFTER UPDATE AS
    	UPDATE Template set TimeStamp=GetDate() WHERE ID in (select id from inserted)
    GO
    
    Create Table Incident
    (
    	ID bigint
    		PRIMARY KEY,
 	OwnerId uniqueidentifier
    		NOT NULL,
	EntityManagerOwningUser uniqueidentifier
    		NOT NULL,
  	IncidentId uniqueidentifier
    		 NOT NULL,
    	Title nvarchar(200),
    	State int 
    		DEFAULT(0) NOT NULL,
    	TimeStamp datetime
    		DEFAULT(GETDATE()) NOT NULL
    )
    GO
    
    Create Trigger TimeStampUpdateIncident
    	on Incident
    AFTER UPDATE AS
    	UPDATE Incident set TimeStamp=GetDate() WHERE ID in (select id from inserted)
    GO
    
    Create Table Annotation
    (
    	ID bigint
    		PRIMARY KEY,
 	OwnerId uniqueidentifier
    		NOT NULL,
	EntityManagerOwningUser uniqueidentifier
    		NOT NULL,
 	AnnotationId uniqueidentifier
    		 NOT NULL,
 	ObjectId uniqueidentifier,
    	ObjectTypeCode int,
    	State int 
    		DEFAULT(0) NOT NULL,
    	TimeStamp datetime
    		DEFAULT(GETDATE()) NOT NULL
    )
    GO
    
    Create Trigger TimeStampUpdateAnnotation
    	on Annotation
    AFTER UPDATE AS
    	UPDATE Annotation set TimeStamp=GetDate() WHERE ID in (select id from inserted)
    GO

    CREATE NONCLUSTERED INDEX [NC_state_objectid] ON [dbo].[Annotation] 
    (
	[ObjectId] ASC,
	[State] ASC
    )WITH (STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = OFF) ON [PRIMARY]
    GO    

    Create Table PriceLevel
    (
    	ID bigint
    		PRIMARY KEY,
  	OrganizationId uniqueidentifier
    		NOT NULL,
	EntityManagerOwningUser uniqueidentifier
    		NOT NULL,
    	PriceLevelId uniqueidentifier
    		 NOT NULL,
    	State int 
    		DEFAULT(0) NOT NULL,
    	TimeStamp datetime
    		DEFAULT(GETDATE()) NOT NULL
    )
    GO 
    
    Create Trigger TimeStampUpdatePriceLevel
    	on PriceLevel
    AFTER UPDATE AS
    	UPDATE PriceLevel set TimeStamp=GetDate() WHERE ID in (select id from inserted)
    GO
    
    Create Table List
    (
    	ID bigint
    		PRIMARY KEY,
    	OwningUser uniqueidentifier
    		NOT NULL,
	EntityManagerOwningUser uniqueidentifier
    		NOT NULL,
 	ListId uniqueidentifier
    		 NOT NULL,
    	State int 
    		DEFAULT(0) NOT NULL,
    	TimeStamp datetime
    		DEFAULT(GETDATE()) NOT NULL
    )
    GO
    
    Create Trigger TimeStampUpdateList
    	on List
    AFTER UPDATE AS
    	UPDATE List set TimeStamp=GetDate() WHERE ID in (select id from inserted)
    GO
    
    Create Table BusinessUnit
    (
    	ID bigint
    		PRIMARY KEY,
 	OrganizationId uniqueidentifier
    		NOT NULL,
	EntityManagerOwningUser uniqueidentifier
    		NOT NULL,
 	BusinessUnitId uniqueidentifier
    		NOT NULL,
    	ParentBusinessUnitId uniqueidentifier,
    	Name nvarchar(160)
    		NOT NULL,
    	State int 
    		DEFAULT(0) NOT NULL,
    	TimeStamp datetime
    		DEFAULT(GETDATE()) NOT NULL
    )
    GO
    
    Create Trigger TimeStampUpdateBusinessUnit
    	on BusinessUnit
    AFTER UPDATE AS
    	UPDATE BusinessUnit set TimeStamp=GetDate() WHERE ID in (select id from inserted)
    GO
    
    Create Table Campaign
    (
    	ID bigint
    		PRIMARY KEY,
    	OwningUser uniqueidentifier
    		NOT NULL,
	EntityManagerOwningUser uniqueidentifier
    		NOT NULL,
 	CampaignId uniqueidentifier
    		 NOT NULL,
    	State int 
    		DEFAULT(0) NOT NULL,
    	TimeStamp datetime
    		DEFAULT(GETDATE()) NOT NULL
    )
    GO
    
    Create Trigger TimeStampUpdateCampaign
    	on Campaign
    AFTER UPDATE AS
    	UPDATE Campaign set TimeStamp=GetDate() WHERE ID in (select id from inserted)
    GO
    
    Create Table ServiceAppointment
    (
    	ID bigint
    		PRIMARY KEY,
    	OwningUser uniqueidentifier
    		NOT NULL,
	EntityManagerOwningUser uniqueidentifier
    		NOT NULL,
 	ActivityId uniqueidentifier
    		 NOT NULL,
    	State int 
    		DEFAULT(0) NOT NULL,
    	TimeStamp datetime
    		DEFAULT(GETDATE()) NOT NULL
    )
    GO
    
    Create Trigger TimeStampUpdateServiceAppointment
    	on ServiceAppointment
    AFTER UPDATE AS
    	UPDATE ServiceAppointment set TimeStamp=GetDate() WHERE ID in (select id from inserted)
    GO
    
    Create Table Role
    (
    	ID bigint
    		PRIMARY KEY,
 	OrganizationId uniqueidentifier
    		NOT NULL,
	EntityManagerOwningUser uniqueidentifier
    		NOT NULL,
 	RoleId uniqueidentifier
    		 NOT NULL,
    	Name nvarchar(100)
    		NOT NULL,
    	State int 
    		DEFAULT(0) NOT NULL,
    	TimeStamp datetime
    		DEFAULT(GETDATE()) NOT NULL
    )
    GO
    
    Create Trigger TimeStampUpdateRole
    	on Role
    AFTER UPDATE AS
    	UPDATE Role set TimeStamp=GetDate() WHERE ID in (select id from inserted)
    GO
    
    Create Table Privilege
    (
    	ID bigint
    		PRIMARY KEY,
 	OrganizationId uniqueidentifier
    		NOT NULL,
 	PrivilegeId uniqueidentifier
    		 NOT NULL,
    	Name nvarchar(100),
    	State int 
    		DEFAULT(0) NOT NULL,
    	TimeStamp datetime
    		DEFAULT(GETDATE()) NOT NULL	
    )
    GO
    
    Create Trigger TimeStampUpdatePrivilege
    	on Privilege
    AFTER UPDATE AS
    	UPDATE Privilege set TimeStamp=GetDate() WHERE ID in (select id from inserted)
    GO
    
  Create Table Equipment
  (
  	ID bigint
  		PRIMARY KEY,
  	OrganizationId uniqueidentifier
  		NOT NULL,
	EntityManagerOwningUser uniqueidentifier
    		NOT NULL,
  	EquipmentId uniqueidentifier
  		 NOT NULL,
  	Name nvarchar(100)
  		NOT NULL,
  	State int 
  		DEFAULT(0) NOT NULL,
  	TimeStamp datetime
  		DEFAULT(GETDATE()) NOT NULL
  )
  GO
  
  Create Trigger TimeStampUpdateEquipment
  	on Equipment
  AFTER UPDATE AS
  	UPDATE Equipment set TimeStamp=GetDate() WHERE ID in (select id from inserted)
  GO

